﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using doiso;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace QuanLyPhongKham
{
	public class frmTrathuoc : BaseUserControl
    {
        private System.Windows.Forms.Label label10;
		private System.Windows.Forms.ComboBox makho;
        private LibList.List listDMBD;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label lTen;
		private System.Windows.Forms.Label label16;
		private System.Windows.Forms.Label label17;
		private System.Windows.Forms.Label label18;
		private System.Windows.Forms.TextBox tenbd;
        private System.Windows.Forms.TextBox mabd;
		private System.Windows.Forms.ComboBox cmbSophieu;
        private string user, s_mmyy, s_ngay, sql, s_loai, s_ngaysp, s_userid;
        private int i_userid, i_mabd, i_madv;
		private long l_id;
		private decimal d_soluong,d_dongia,d_sotien,d_giaban;
        private bool bNew, bEdit;
		private DAL.AccessData d;
		private Doisototext doiso=new Doisototext();
		private DataTable dtdmbd=new DataTable();
		private DataTable dtll=new DataTable();
		private DataTable dtct=new DataTable();
		private DataTable dtsave=new DataTable();
        private DataTable dtxoa = new DataTable();
        private DataTable dtthem = new DataTable();
		private System.Windows.Forms.Label ldvt;
		private DataRow r;
		private System.Windows.Forms.TextBox stt;
		private System.Windows.Forms.TextBox tenhc;
		private System.Windows.Forms.Label lTenhc;
        private MaskedTextBox.MaskedTextBox sophieu;
		private System.Windows.Forms.Label label25;
		private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();
		private PrintDialog p=new PrintDialog();
        private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox manguon;
		private System.Windows.Forms.Label label9;
        private System.Windows.Forms.Label lbl;
        private Label label4;
        private ComboBox khoban;
        private Label label6;
        private DataGrid dataGrid1;
        private System.Windows.Forms.MaskedTextBox ngayban;
        private Label label1;
        private Panel bNut;
        private Button butKetthuc;
        private Button butThem;
        private Button butChon;
        private Button butXoa;
        private Button butHuy;
        private Button butSua;
        private Button butMoi;
        private Button butLuu;
        private Button butIn;
        private Button butBoqua;
        private CheckedListBox chksodon;
        private TextBox dvt;
        private TextBox soluong;
        private TextBox dongia;
        private TextBox sotien;
        private TextBox giaban;
        private TextBox hoten;
        private TextBox namsinh;
		private System.ComponentModel.Container components = null;
        public frmTrathuoc(DAL.AccessData acc, string loai, string mmyy, string ngay, int userid, string user)
		{
			InitializeComponent();
			d=acc;
			i_userid=userid;s_mmyy=mmyy;s_ngay=ngay;
            s_loai = loai; s_userid = user;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.lbl = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.makho = new System.Windows.Forms.ComboBox();
            this.listDMBD = new LibList.List();
            this.label13 = new System.Windows.Forms.Label();
            this.lTen = new System.Windows.Forms.Label();
            this.ldvt = new System.Windows.Forms.Label();
            this.label16 = new System.Windows.Forms.Label();
            this.label17 = new System.Windows.Forms.Label();
            this.label18 = new System.Windows.Forms.Label();
            this.tenbd = new System.Windows.Forms.TextBox();
            this.mabd = new System.Windows.Forms.TextBox();
            this.cmbSophieu = new System.Windows.Forms.ComboBox();
            this.stt = new System.Windows.Forms.TextBox();
            this.tenhc = new System.Windows.Forms.TextBox();
            this.lTenhc = new System.Windows.Forms.Label();
            this.sophieu = new MaskedTextBox.MaskedTextBox();
            this.label25 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.manguon = new System.Windows.Forms.ComboBox();
            this.label9 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.khoban = new System.Windows.Forms.ComboBox();
            this.label6 = new System.Windows.Forms.Label();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.ngayban = new System.Windows.Forms.MaskedTextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.bNut = new System.Windows.Forms.Panel();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butThem = new System.Windows.Forms.Button();
            this.butChon = new System.Windows.Forms.Button();
            this.butXoa = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butMoi = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butIn = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.chksodon = new System.Windows.Forms.CheckedListBox();
            this.dvt = new System.Windows.Forms.TextBox();
            this.soluong = new System.Windows.Forms.TextBox();
            this.dongia = new System.Windows.Forms.TextBox();
            this.sotien = new System.Windows.Forms.TextBox();
            this.giaban = new System.Windows.Forms.TextBox();
            this.hoten = new System.Windows.Forms.TextBox();
            this.namsinh = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.bNut.SuspendLayout();
            this.SuspendLayout();
            // 
            // lbl
            // 
            this.lbl.Location = new System.Drawing.Point(129, 6);
            this.lbl.Name = "lbl";
            this.lbl.Size = new System.Drawing.Size(56, 20);
            this.lbl.TabIndex = 2;
            this.lbl.Text = "Toa số :";
            this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label10
            // 
            this.label10.Location = new System.Drawing.Point(270, 29);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(64, 23);
            this.label10.TabIndex = 12;
            this.label10.Text = "Kho nhận :";
            this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // makho
            // 
            this.makho.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.makho.BackColor = System.Drawing.SystemColors.HighlightText;
            this.makho.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.makho.Enabled = false;
            this.makho.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.makho.Location = new System.Drawing.Point(332, 28);
            this.makho.Name = "makho";
            this.makho.Size = new System.Drawing.Size(295, 21);
            this.makho.TabIndex = 13;
            this.makho.KeyDown += new System.Windows.Forms.KeyEventHandler(this.makho_KeyDown);
            // 
            // listDMBD
            // 
            this.listDMBD.BackColor = System.Drawing.SystemColors.Info;
            this.listDMBD.ColumnCount = 0;
            this.listDMBD.Location = new System.Drawing.Point(376, 544);
            this.listDMBD.MatchBufferTimeOut = 1000;
            this.listDMBD.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
            this.listDMBD.Name = "listDMBD";
            this.listDMBD.Size = new System.Drawing.Size(75, 17);
            this.listDMBD.TabIndex = 26;
            this.listDMBD.TextIndex = -1;
            this.listDMBD.TextMember = null;
            this.listDMBD.ValueIndex = -1;
            this.listDMBD.Visible = false;
            this.listDMBD.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listDMBD_KeyDown);
            // 
            // label13
            // 
            this.label13.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label13.Location = new System.Drawing.Point(-5, 353);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(69, 23);
            this.label13.TabIndex = 14;
            this.label13.Text = "Mã thuốc :";
            this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lTen
            // 
            this.lTen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lTen.Location = new System.Drawing.Point(114, 353);
            this.lTen.Name = "lTen";
            this.lTen.Size = new System.Drawing.Size(73, 24);
            this.lTen.TabIndex = 16;
            this.lTen.Text = "Tên thuốc :";
            this.lTen.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ldvt
            // 
            this.ldvt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.ldvt.Location = new System.Drawing.Point(6, 376);
            this.ldvt.Name = "ldvt";
            this.ldvt.Size = new System.Drawing.Size(58, 23);
            this.ldvt.TabIndex = 20;
            this.ldvt.Text = "ĐVT :";
            this.ldvt.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label16
            // 
            this.label16.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label16.Location = new System.Drawing.Point(131, 376);
            this.label16.Name = "label16";
            this.label16.Size = new System.Drawing.Size(56, 23);
            this.label16.TabIndex = 22;
            this.label16.Text = "Số lượng :";
            this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label17
            // 
            this.label17.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label17.Location = new System.Drawing.Point(244, 376);
            this.label17.Name = "label17";
            this.label17.Size = new System.Drawing.Size(56, 23);
            this.label17.TabIndex = 24;
            this.label17.Text = "Giá mua :";
            this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label18
            // 
            this.label18.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label18.Location = new System.Drawing.Point(378, 376);
            this.label18.Name = "label18";
            this.label18.Size = new System.Drawing.Size(56, 23);
            this.label18.TabIndex = 26;
            this.label18.Text = "Số tiền :";
            this.label18.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tenbd
            // 
            this.tenbd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tenbd.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tenbd.Enabled = false;
            this.tenbd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tenbd.Location = new System.Drawing.Point(185, 353);
            this.tenbd.Name = "tenbd";
            this.tenbd.Size = new System.Drawing.Size(248, 21);
            this.tenbd.TabIndex = 17;
            this.tenbd.TextChanged += new System.EventHandler(this.tenbd_TextChanged);
            this.tenbd.Validated += new System.EventHandler(this.tenbd_Validated);
            this.tenbd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tenbd_KeyDown);
            // 
            // mabd
            // 
            this.mabd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.mabd.BackColor = System.Drawing.SystemColors.HighlightText;
            this.mabd.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.mabd.Enabled = false;
            this.mabd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.mabd.Location = new System.Drawing.Point(62, 353);
            this.mabd.Name = "mabd";
            this.mabd.Size = new System.Drawing.Size(63, 21);
            this.mabd.TabIndex = 15;
            this.mabd.TextChanged += new System.EventHandler(this.mabd_TextChanged);
            this.mabd.Validated += new System.EventHandler(this.mabd_Validated);
            this.mabd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tenbd_KeyDown);
            // 
            // cmbSophieu
            // 
            this.cmbSophieu.BackColor = System.Drawing.Color.White;
            this.cmbSophieu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbSophieu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmbSophieu.Location = new System.Drawing.Point(184, 5);
            this.cmbSophieu.Name = "cmbSophieu";
            this.cmbSophieu.Size = new System.Drawing.Size(82, 21);
            this.cmbSophieu.TabIndex = 3;
            this.cmbSophieu.SelectedIndexChanged += new System.EventHandler(this.cmbSophieu_SelectedIndexChanged);
            this.cmbSophieu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cmbSophieu_KeyDown);
            // 
            // stt
            // 
            this.stt.Enabled = false;
            this.stt.Location = new System.Drawing.Point(64, 288);
            this.stt.Name = "stt";
            this.stt.Size = new System.Drawing.Size(40, 20);
            this.stt.TabIndex = 60;
            // 
            // tenhc
            // 
            this.tenhc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.tenhc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tenhc.Enabled = false;
            this.tenhc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tenhc.Location = new System.Drawing.Point(491, 353);
            this.tenhc.Name = "tenhc";
            this.tenhc.Size = new System.Drawing.Size(136, 21);
            this.tenhc.TabIndex = 19;
            // 
            // lTenhc
            // 
            this.lTenhc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.lTenhc.Location = new System.Drawing.Point(429, 352);
            this.lTenhc.Name = "lTenhc";
            this.lTenhc.Size = new System.Drawing.Size(63, 23);
            this.lTenhc.TabIndex = 18;
            this.lTenhc.Text = "Hoạt chất :";
            this.lTenhc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // sophieu
            // 
            this.sophieu.BackColor = System.Drawing.SystemColors.HighlightText;
            this.sophieu.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.sophieu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sophieu.Location = new System.Drawing.Point(184, 5);
            this.sophieu.Masked = MaskedTextBox.MaskedTextBox.Mask.None;
            this.sophieu.Name = "sophieu";
            this.sophieu.Size = new System.Drawing.Size(82, 21);
            this.sophieu.TabIndex = 3;
            this.sophieu.Validated += new System.EventHandler(this.sophieu_Validated);
            this.sophieu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ngayban_KeyDown);
            // 
            // label25
            // 
            this.label25.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label25.Location = new System.Drawing.Point(522, 376);
            this.label25.Name = "label25";
            this.label25.Size = new System.Drawing.Size(56, 23);
            this.label25.TabIndex = 28;
            this.label25.Text = "Giá bán :";
            this.label25.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(266, -27);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(48, 23);
            this.label3.TabIndex = 4;
            this.label3.Text = "Họ tên :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label3.Visible = false;
            // 
            // manguon
            // 
            this.manguon.BackColor = System.Drawing.SystemColors.HighlightText;
            this.manguon.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.manguon.Enabled = false;
            this.manguon.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.manguon.Location = new System.Drawing.Point(62, 28);
            this.manguon.Name = "manguon";
            this.manguon.Size = new System.Drawing.Size(204, 21);
            this.manguon.TabIndex = 11;
            this.manguon.KeyDown += new System.Windows.Forms.KeyEventHandler(this.manguon_KeyDown);
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(17, 30);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(48, 20);
            this.label9.TabIndex = 10;
            this.label9.Text = "Nguồn :";
            this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(2, 9);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(62, 17);
            this.label4.TabIndex = 0;
            this.label4.Text = "Ngày bán :";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // khoban
            // 
            this.khoban.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.khoban.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khoban.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.khoban.Enabled = false;
            this.khoban.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khoban.Location = new System.Drawing.Point(332, 5);
            this.khoban.Name = "khoban";
            this.khoban.Size = new System.Drawing.Size(295, 21);
            this.khoban.TabIndex = 9;
            this.khoban.KeyDown += new System.Windows.Forms.KeyEventHandler(this.khoban_KeyDown);
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(274, 7);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(60, 18);
            this.label6.TabIndex = 8;
            this.label6.Text = "Kho bán :";
            this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.Color.White;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.Color.White;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.CaptionVisible = false;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(2, 52);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.RowHeaderWidth = 5;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(624, 295);
            this.dataGrid1.TabIndex = 77;
            // 
            // ngayban
            // 
            this.ngayban.BackColor = System.Drawing.Color.White;
            this.ngayban.Location = new System.Drawing.Point(62, 6);
            this.ngayban.Mask = "00/00/0000";
            this.ngayban.Name = "ngayban";
            this.ngayban.Size = new System.Drawing.Size(72, 20);
            this.ngayban.TabIndex = 1;
            this.ngayban.ValidatingType = typeof(System.DateTime);
            this.ngayban.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ngayban_KeyDown);
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.Location = new System.Drawing.Point(506, -27);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(69, 23);
            this.label1.TabIndex = 6;
            this.label1.Text = "Năm sinh :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.label1.Visible = false;
            // 
            // bNut
            // 
            this.bNut.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.bNut.Controls.Add(this.butKetthuc);
            this.bNut.Controls.Add(this.butThem);
            this.bNut.Controls.Add(this.butChon);
            this.bNut.Controls.Add(this.butXoa);
            this.bNut.Controls.Add(this.butHuy);
            this.bNut.Controls.Add(this.butSua);
            this.bNut.Controls.Add(this.butMoi);
            this.bNut.Controls.Add(this.butLuu);
            this.bNut.Controls.Add(this.butIn);
            this.bNut.Controls.Add(this.butBoqua);
            this.bNut.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.bNut.Location = new System.Drawing.Point(2, 431);
            this.bNut.Name = "bNut";
            this.bNut.Size = new System.Drawing.Size(625, 34);
            this.bNut.TabIndex = 81;
            this.bNut.SizeChanged += new System.EventHandler(this.bNut_SizeChanged);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKetthuc.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
            this.butKetthuc.Image = global::QuanLyPhongKham.Properties.Resources.butKetthuc1;
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(545, 2);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(78, 31);
            this.butKetthuc.TabIndex = 8;
            this.butKetthuc.Text = "      &Kết thúc";
            this.butKetthuc.UseVisualStyleBackColor = true;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butThem
            // 
            this.butThem.Image = global::QuanLyPhongKham.Properties.Resources.butThem;
            this.butThem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThem.Location = new System.Drawing.Point(126, 2);
            this.butThem.Name = "butThem";
            this.butThem.Size = new System.Drawing.Size(63, 31);
            this.butThem.TabIndex = 0;
            this.butThem.Text = "     &Thêm";
            this.butThem.UseVisualStyleBackColor = true;
            this.butThem.Click += new System.EventHandler(this.butThem_Click);
            // 
            // butChon
            // 
            this.butChon.Image = global::QuanLyPhongKham.Properties.Resources.Ok;
            this.butChon.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butChon.Location = new System.Drawing.Point(72, 38);
            this.butChon.Name = "butChon";
            this.butChon.Size = new System.Drawing.Size(83, 31);
            this.butChon.TabIndex = 60;
            this.butChon.Text = "    &Đơn thuốc";
            this.butChon.UseVisualStyleBackColor = true;
            this.butChon.Visible = false;
            // 
            // butXoa
            // 
            this.butXoa.Image = global::QuanLyPhongKham.Properties.Resources.butXoa2;
            this.butXoa.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butXoa.Location = new System.Drawing.Point(189, 2);
            this.butXoa.Name = "butXoa";
            this.butXoa.Size = new System.Drawing.Size(62, 31);
            this.butXoa.TabIndex = 3;
            this.butXoa.Text = "     &Xóa";
            this.butXoa.UseVisualStyleBackColor = true;
            this.butXoa.Click += new System.EventHandler(this.butXoa_Click);
            // 
            // butHuy
            // 
            this.butHuy.Image = global::QuanLyPhongKham.Properties.Resources.butXoa;
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(388, 2);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(65, 31);
            this.butHuy.TabIndex = 6;
            this.butHuy.Text = "   &Hủy";
            this.butHuy.UseVisualStyleBackColor = true;
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butSua
            // 
            this.butSua.Image = global::QuanLyPhongKham.Properties.Resources.butSua;
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(251, 2);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(62, 31);
            this.butSua.TabIndex = 4;
            this.butSua.Text = "     &Sửa";
            this.butSua.UseVisualStyleBackColor = true;
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butMoi
            // 
            this.butMoi.Image = global::QuanLyPhongKham.Properties.Resources.butMoi;
            this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butMoi.Location = new System.Drawing.Point(2, 2);
            this.butMoi.Name = "butMoi";
            this.butMoi.Size = new System.Drawing.Size(62, 31);
            this.butMoi.TabIndex = 2;
            this.butMoi.Text = "     &Mới";
            this.butMoi.UseVisualStyleBackColor = true;
            this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
            // 
            // butLuu
            // 
            this.butLuu.Image = global::QuanLyPhongKham.Properties.Resources.butLuu1;
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(64, 2);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(62, 31);
            this.butLuu.TabIndex = 1;
            this.butLuu.Text = "     &Lưu";
            this.butLuu.UseVisualStyleBackColor = true;
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butIn
            // 
            this.butIn.Image = global::QuanLyPhongKham.Properties.Resources.butIn;
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(453, 2);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(92, 31);
            this.butIn.TabIndex = 7;
            this.butIn.Text = "      &Phiếu trả";
            this.butIn.UseVisualStyleBackColor = true;
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Image = global::QuanLyPhongKham.Properties.Resources.butBoqua;
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(313, 2);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(75, 31);
            this.butBoqua.TabIndex = 5;
            this.butBoqua.Text = "     &Bỏ qua";
            this.butBoqua.UseVisualStyleBackColor = true;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // chksodon
            // 
            this.chksodon.FormattingEnabled = true;
            this.chksodon.Location = new System.Drawing.Point(62, 51);
            this.chksodon.Name = "chksodon";
            this.chksodon.Size = new System.Drawing.Size(565, 94);
            this.chksodon.TabIndex = 82;
            this.chksodon.Visible = false;
            // 
            // dvt
            // 
            this.dvt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.dvt.BackColor = System.Drawing.SystemColors.HighlightText;
            this.dvt.Enabled = false;
            this.dvt.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dvt.Location = new System.Drawing.Point(62, 376);
            this.dvt.Name = "dvt";
            this.dvt.Size = new System.Drawing.Size(63, 21);
            this.dvt.TabIndex = 21;
            // 
            // soluong
            // 
            this.soluong.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.soluong.BackColor = System.Drawing.SystemColors.HighlightText;
            this.soluong.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.soluong.Enabled = false;
            this.soluong.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.soluong.Location = new System.Drawing.Point(185, 376);
            this.soluong.Name = "soluong";
            this.soluong.Size = new System.Drawing.Size(63, 21);
            this.soluong.TabIndex = 23;
            this.soluong.Validated += new System.EventHandler(this.soluong_Validated);
            this.soluong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ngayban_KeyDown);
            // 
            // dongia
            // 
            this.dongia.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.dongia.BackColor = System.Drawing.SystemColors.HighlightText;
            this.dongia.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.dongia.Enabled = false;
            this.dongia.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dongia.Location = new System.Drawing.Point(297, 376);
            this.dongia.Name = "dongia";
            this.dongia.Size = new System.Drawing.Size(90, 21);
            this.dongia.TabIndex = 25;
            this.dongia.Validated += new System.EventHandler(this.dongia_Validated);
            this.dongia.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ngayban_KeyDown);
            // 
            // sotien
            // 
            this.sotien.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.sotien.BackColor = System.Drawing.SystemColors.HighlightText;
            this.sotien.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.sotien.Enabled = false;
            this.sotien.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sotien.Location = new System.Drawing.Point(432, 376);
            this.sotien.Name = "sotien";
            this.sotien.Size = new System.Drawing.Size(97, 21);
            this.sotien.TabIndex = 27;
            this.sotien.Validated += new System.EventHandler(this.sotien_Validated);
            this.sotien.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ngayban_KeyDown);
            // 
            // giaban
            // 
            this.giaban.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.giaban.BackColor = System.Drawing.SystemColors.HighlightText;
            this.giaban.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.giaban.Enabled = false;
            this.giaban.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.giaban.Location = new System.Drawing.Point(576, 376);
            this.giaban.Name = "giaban";
            this.giaban.Size = new System.Drawing.Size(51, 21);
            this.giaban.TabIndex = 29;
            this.giaban.Validated += new System.EventHandler(this.giaban_Validated);
            this.giaban.KeyDown += new System.Windows.Forms.KeyEventHandler(this.giaban_KeyDown);
            // 
            // hoten
            // 
            this.hoten.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.hoten.BackColor = System.Drawing.SystemColors.HighlightText;
            this.hoten.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.hoten.Enabled = false;
            this.hoten.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.hoten.Location = new System.Drawing.Point(313, -27);
            this.hoten.Name = "hoten";
            this.hoten.Size = new System.Drawing.Size(205, 21);
            this.hoten.TabIndex = 5;
            this.hoten.Visible = false;
            // 
            // namsinh
            // 
            this.namsinh.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.namsinh.BackColor = System.Drawing.SystemColors.HighlightText;
            this.namsinh.Enabled = false;
            this.namsinh.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.namsinh.Location = new System.Drawing.Point(574, -27);
            this.namsinh.Name = "namsinh";
            this.namsinh.Size = new System.Drawing.Size(53, 21);
            this.namsinh.TabIndex = 7;
            this.namsinh.Visible = false;
            // 
            // frmTrathuoc
            // 
            //this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(628, 470);
            this.Controls.Add(this.dataGrid1);
            this.Controls.Add(this.giaban);
            this.Controls.Add(this.sotien);
            this.Controls.Add(this.dongia);
            this.Controls.Add(this.soluong);
            this.Controls.Add(this.dvt);
            this.Controls.Add(this.bNut);
            this.Controls.Add(this.ngayban);
            this.Controls.Add(this.khoban);
            this.Controls.Add(this.manguon);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.cmbSophieu);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.sophieu);
            this.Controls.Add(this.makho);
            this.Controls.Add(this.label10);
            this.Controls.Add(this.lbl);
            this.Controls.Add(this.tenbd);
            this.Controls.Add(this.tenhc);
            this.Controls.Add(this.lTenhc);
            this.Controls.Add(this.mabd);
            this.Controls.Add(this.label18);
            this.Controls.Add(this.label17);
            this.Controls.Add(this.label16);
            this.Controls.Add(this.ldvt);
            this.Controls.Add(this.lTen);
            this.Controls.Add(this.label13);
            this.Controls.Add(this.listDMBD);
            this.Controls.Add(this.stt);
            this.Controls.Add(this.label25);
            this.Controls.Add(this.chksodon);
            this.Controls.Add(this.namsinh);
            this.Controls.Add(this.hoten);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label1);
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "frmTrathuoc";
            //this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Phiếu trả thuốc";
            this.Load += new System.EventHandler(this.frmTrathuoc_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.bNut.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmTrathuoc_Load(object sender, System.EventArgs e)
		{
            user = d.user; 
			manguon.DisplayMember="TEN";
			manguon.ValueMember="ID";
			manguon.DataSource=d.get_data("select id,ten from "+user+".d_dmnguon order by id").Tables[0];
            //
			makho.DisplayMember="TEN";
			makho.ValueMember="ID";
            sql = "select id,ten from " + user + ".d_dmkho order by id";
			makho.DataSource=d.get_data(sql).Tables[0];
			makho.SelectedIndex=0;

            khoban.DisplayMember = "TEN";
            khoban.ValueMember = "ID";
            sql = "select id,ten from " + user + ".d_dmkho order by id";
            khoban.DataSource = d.get_data(sql).Tables[0];
            khoban.SelectedIndex = 0;

			listDMBD.DisplayMember="TEN";
			listDMBD.ValueMember="MA";
            
			cmbSophieu.DisplayMember="SOPHIEU";
			cmbSophieu.ValueMember="ID";
            load_sophieu();
            l_id = (cmbSophieu.SelectedIndex != -1) ? long.Parse(cmbSophieu.SelectedValue.ToString()) : 0;
            if (l_id == 0) load_grid();
			AddGridTableStyle();
			ref_text();
            cmbSophieu.Focus();
            ena_object(false);
		}
        private void load_sophieu()
        {
            sql = "select * from " + user + ".d_nhapll_" + s_mmyy + " where loai='" + s_loai + "'";
            sql += " and to_char(ngaysp,'dd/mm/yyyy')='" + s_ngay + "'";
            sql += " and userid=" + i_userid;
            sql += " order by id";
            dtll = d.get_data(sql).Tables[0];
            cmbSophieu.DataSource = dtll;
        }
		private void load_grid()
		{
            sql = "select a.stt,a.mabd,a.madv,b.ma,trim(b.ten)||' '||b.hamluong as ten,nullif(b.tenhc,' ') as tenhc,b.dang,a.handung,a.losx,0 as madv,a.soluong,a.dongia,a.vat,a.sotien,round(a.sotien+a.sotien*a.vat/100,3) as sotienvat,a.giaban,a.giamua,a.sl1,a.sl2,a.tyle,a.giabancu,a.giamuacu,round(a.giamua*a.soluong,3) as tongtien,a.tyle_ggia,a.st_ggia ";
            sql += " from " + user + ".d_nhapct_"+s_mmyy+" a," + user + ".d_dmbd b where a.mabd=b.id and a.id=" + l_id + " order by a.stt";
			dtct=d.get_data(sql).Tables[0];
            dtxoa = dtct.Clone(); dtthem = dtct.Clone(); dtsave = dtct.Clone();
			dataGrid1.DataSource=dtct;
		}
        private void load_grid_thuocbn()
        {
            sql = "select b.stt,b.sttt,b.mabd,c.ma,trim(c.ten)||' '||c.hamluong as ten,nullif(c.tenhc,' ') as tenhc,c.dang,b.handung,b.madv,b.losx,b.soluong,b.giamua as dongia,0 as vat,round(b.giamua*b.soluong,3) as sotien,round(b.giamua*b.soluong,3) as sotienvat,b.giaban,b.giamua,b.soluong as sl1,1 as sl2,0 tyle,b.giaban as giabancu,b.giamua as giamuacu,round(b.giamua*b.soluong,3) as tongtien,0 as tyle_ggia,0 as st_ggia ";
            sql += " from " + user + ".d_toathuocll_" + s_mmyy + " a," + user + ".d_toathuocct_" + s_mmyy + " b," + user + ".d_dmbd c where a.id=b.id and b.mabd=c.id and a.mabn='" + sophieu.Text.Trim() + "' order by b.stt";
            foreach (DataRow r1 in d.get_data(sql).Tables[0].Rows)
            {
                d.updrec_nhapct(dtct, long.Parse(r1["stt"].ToString()), int.Parse(r1["mabd"].ToString()), r1["ma"].ToString(), r1["ten"].ToString(), r1["tenhc"].ToString(), r1["dang"].ToString(), r1["handung"].ToString(), r1["losx"].ToString(), int.Parse(r1["madv"].ToString()) , decimal.Parse(r1["soluong"].ToString()), 
                    decimal.Parse(r1["dongia"].ToString()), decimal.Parse(r1["sotien"].ToString()), int.Parse(r1["vat"].ToString()), decimal.Parse(r1["sotienvat"].ToString()), decimal.Parse(r1["giaban"].ToString()), decimal.Parse(r1["giamua"].ToString()), decimal.Parse(r1["sl1"].ToString()), decimal.Parse(r1["sl2"].ToString()), 0, decimal.Parse(r1["giabancu"].ToString()), decimal.Parse(r1["giamuacu"].ToString()), decimal.Parse(r1["tongtien"].ToString()));
            }
            dtct.AcceptChanges();
        }
		private void ref_text()
		{
			try
			{
				int i=dataGrid1.CurrentCell.RowNumber;
				stt.Text=dataGrid1[i,0].ToString();
				mabd.Text=dataGrid1[i,1].ToString();
				tenbd.Text=dataGrid1[i,2].ToString();
				tenhc.Text=dataGrid1[i,3].ToString();
				dvt.Text=dataGrid1[i,4].ToString();
				d_soluong=(dataGrid1[i,5].ToString()!="")?decimal.Parse(dataGrid1[i,5].ToString()):0;
				d_dongia=(dataGrid1[i,6].ToString()!="")?decimal.Parse(dataGrid1[i,6].ToString()):0;
				d_sotien=(dataGrid1[i,7].ToString()!="")?decimal.Parse(dataGrid1[i,7].ToString()):0;
				d_giaban=(dataGrid1[i,8].ToString()!="")?decimal.Parse(dataGrid1[i,8].ToString()):0;
                soluong.Text = d_soluong.ToString("#,###,###,##0.0");
                dongia.Text = d_dongia.ToString("#,###,###,##0.00");
                sotien.Text = d_sotien.ToString("#,###,###,##0.00");
                giaban.Text = d_giaban.ToString("#,###,###,##0.00");
			}
			catch{}
		}

		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dtct.TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.RowHeaderWidth = 5;
            ts.AllowSorting = false;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stt";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ma";
			TextCol.HeaderText = "Mã số";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ten";
            TextCol.HeaderText = "Tên thuốc - hàm lượng";
			TextCol.Width = 300;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tenhc";
			TextCol.HeaderText = "Hoạt chất";
			TextCol.Width = 210;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "dang";
			TextCol.HeaderText = "ĐVT";
			TextCol.Width = 60;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "soluong";
			TextCol.HeaderText = "Số lượng";
			TextCol.Width = 100;
            TextCol.Format = "###,###,###,##0.0";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "dongia";
			TextCol.HeaderText = "Giá mua";
			TextCol.Width = 120;
            TextCol.Format = "###,###,###,##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Width = 120;
			TextCol.Format="###,###,###,##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "giaban";
			TextCol.HeaderText = "Giá bán";
			TextCol.Width = 120;
			TextCol.Format="###,###,###,##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}

		private void load_dm()
		{
            dtdmbd = d.get_data("select ma,trim(ten)||' '||hamluong as ten,tenhc,trim(dang)||'/'||trim(donvi) as dang,madv,id,dongia,giaban from " + user + ".d_dmbd order by ten").Tables[0];
			listDMBD.DataSource=dtdmbd;
		}

		private void sophieu_Validated(object sender, System.EventArgs e)
		{
			if (l_id!=0) return;
			try
			{
                if (sophieu.Text.Trim() == "")
                {
                    MessageBox.Show("Nhập số toa đã bán !");
                    return;
                }
                if (d.get_data("select idduyet from " + d.user + ".d_toathuocll_" + d.mmyy(ngayban.Text) + " where idduyet <>0 and mabn='" + sophieu.Text.Trim() + "'").Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("Toa thuốc này đã được trả !");
                    sophieu.Focus();
                    return;
                }
                load_grid_thuocbn();
                if(dtct.Rows.Count>0) butLuu.Focus();
			}
			catch{}
		}
       
		private void cmbSophieu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab) butMoi.Focus();
		}

		private void cmbSophieu_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			try
			{
				l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
				r=d.getrowbyid(dtll,"id="+l_id);
				if (r!=null)
				{
					sophieu.Text=r["sophieu"].ToString();
                    ngayban.Text = r["ngaysp"].ToString();
					s_ngaysp=r["ngaysp"].ToString();
					makho.SelectedValue=r["makho"].ToString();
                    khoban.SelectedValue = r["makho"].ToString();
					manguon.SelectedValue=r["manguon"].ToString();
					hoten.Text=r["nguoigiao"].ToString();
                    namsinh.Text = r["no"].ToString();
                    i_madv = int.Parse(r["madv"].ToString());
				}
			}
			catch{l_id=0;}
			load_grid();
			ref_text();
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			//this.Close();
		}

		private void Filter_dmbd(string ten)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[listDMBD.DataSource];
				DataView dv=(DataView)cm.List;
				dv.RowFilter="ten like '%"+ten.Trim()+"%'"+" or tenhc like '%"+ten.Trim()+"%'";
			}
			catch{}
		}

		private void tenbd_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listDMBD.Focus();
			else if (e.KeyCode==Keys.Enter)
			{
				if (listDMBD.Visible) listDMBD.Focus();
				else SendKeys.Send("{Tab}");
			}
		}

		private void tenbd_Validated(object sender, System.EventArgs e)
		{
			if(!listDMBD.Focused) listDMBD.Hide();
			if (tenbd.Text!="" && mabd.Text=="")
			{
				r=d.getrowbyid(dtdmbd,"ten='"+tenbd.Text+"'");
				if (r!=null)
				{
					mabd.Text=r["ma"].ToString();
					dvt.Text=r["dang"].ToString();
					tenhc.Text=r["tenhc"].ToString();
					d_dongia=decimal.Parse(r["dongia"].ToString());
					d_giaban=decimal.Parse(r["giaban"].ToString());
                    dongia.Text = d_dongia.ToString("###,###,##0.00");
					giaban.Text=d_giaban.ToString("###,###,##0.00");
				}
			}
		}

		private void ena_object(bool ena)
		{
			sophieu.Visible=ena;
			cmbSophieu.Visible=!ena;
			sophieu.Enabled=ena;
			makho.Enabled=ena;
			manguon.Enabled=ena;
            ngayban.Enabled = ena;
			mabd.Enabled=ena;
			tenbd.Enabled=ena;
			soluong.Enabled=ena;
			dongia.Enabled=ena;
			sotien.Enabled=ena;
			giaban.Enabled=ena;
			butMoi.Enabled=!ena;
			butSua.Enabled=!ena;
			butThem.Enabled=ena;
			butXoa.Enabled=ena;
			butLuu.Enabled=ena;
			butBoqua.Enabled=ena;
			butHuy.Enabled=!ena;
			butIn.Enabled=!ena;
			butKetthuc.Enabled=!ena;
        }

		private void emp_head()
		{
			l_id=0;
            sophieu.Text = "";
            hoten.Text = ""; namsinh.Text = "";
			ngayban.Text=s_ngay;
			makho.SelectedIndex=0;
			s_ngaysp=ngayban.Text;
		}
		
		private void emp_detail()
		{
            d_soluong = 0; d_dongia = 0; d_giaban = 0; d_sotien = 0; stt.Text = "";
			mabd.Text="";
			tenbd.Text="";
			tenhc.Text="";
			dvt.Text="";
			soluong.Text="0";
			dongia.Text="0";
			sotien.Text="0";
			giaban.Text="0";
		}
		private void butMoi_Click(object sender, System.EventArgs e)
		{
			if (d.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng"+s_mmyy.Substring(0,2)+" năm "+s_mmyy.Substring(2,2)+" đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống",DAL.AccessData.Msg);
				return;
			}
			ena_object(true);
			dtct.Clear();
            dtsave.Clear(); dtxoa.Clear();
			emp_head();
			emp_detail();
            bNew = true; bEdit = false;
			if (sophieu.Enabled) sophieu.Focus();
		}
		private void butSua_Click(object sender, System.EventArgs e)
		{
			if (cmbSophieu.Items.Count==0) return;
			if (d.bKhoaso(s_mmyy))
			{
                MessageBox.Show("Số liệu tháng" + s_mmyy.Substring(0, 2) + " năm " + s_mmyy.Substring(2, 2) + " đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống", DAL.AccessData.Msg);
				return;
			}
			l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
			ena_object(true);
            bNew = false; bEdit = true;
			dtsave=dtct.Copy();
			dtxoa.Clear();
			sophieu.Focus();
			ref_text();
		}
		private bool KiemtraHead()
		{
			if (sophieu.Text=="")
			{
                MessageBox.Show("Nhập mã bệnh nhân !", DAL.AccessData.Msg);
				sophieu.Focus();
				return false;
			}
			if (ngayban.Text=="")
			{
                MessageBox.Show("Nhập ngày bán !", DAL.AccessData.Msg);
				ngayban.Focus();
				return false;
			}

			if (manguon.SelectedIndex==-1)
			{
				MessageBox.Show("Nhập nguồn hàng !",DAL.AccessData.Msg);
				manguon.Focus();
				return false;
			}

			if (makho.SelectedIndex==-1)
			{
				MessageBox.Show("Nhập kho nhập !",DAL.AccessData.Msg);
				makho.Focus();
				return false;
			}
            if (khoban.SelectedIndex == -1)
            {
                MessageBox.Show("Nhập kho bán !", DAL.AccessData.Msg);
                khoban.Focus();
                return false;
            }
			return true;
		}

		private bool KiemtraDetail()
		{
			i_mabd=0;
			if (mabd.Text=="" || tenbd.Text=="")
			{
				mabd.Focus();
				return false;
			}
			r=d.getrowbyid(dtdmbd,"ma='"+mabd.Text+"'");
			if (r==null)
			{
				MessageBox.Show("Mã số không hợp lệ !",DAL.AccessData.Msg);
				mabd.Focus();
				return false;
			}
			i_mabd=int.Parse(r["id"].ToString());
			if (soluong.Text=="" || soluong.Text=="0.00" || soluong.Text=="0")
			{
				MessageBox.Show("Nhập số lượng !",DAL.AccessData.Msg);
				soluong.Focus();
				return false;
			}
			try
			{
				d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
				d_giaban=(giaban.Text!="")?decimal.Parse(giaban.Text):0;
				if (d_giaban<d_dongia)
				{
                    MessageBox.Show("Giá bán không được nhỏ hơn giá mua !", DAL.AccessData.Msg);
					giaban.Focus();
					return false;
				}
			}
			catch{return false;}
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if (!KiemtraHead()) return;
            l_id = (bNew) ? d.get_capid : l_id;
			if (!d.upd_nhapll(s_mmyy,l_id,sophieu.Text,ngayban.Text,sophieu.Text,ngayban.Text,s_loai,hoten.Text,i_madv,int.Parse(makho.SelectedValue.ToString()),int.Parse(manguon.SelectedValue.ToString()),namsinh.Text,"",i_userid,0))
			{
				MessageBox.Show("Không cập nhật được thông tin trả thuốc !",DAL.AccessData.Msg);
				return;
			}
			if (!bNew)
			{
                foreach (DataRow r1 in dtct.Rows)
                {
                    DataRow nr = d.getrowbyid(dtsave, "id=" + int.Parse(r1["id"].ToString()));
                    if (nr != null)
                    {
                        decimal d_soluong_old = decimal.Parse(nr["soluong"].ToString());
                        if (d_soluong_old != decimal.Parse(r1["soluong"].ToString()))
                            d.upd_theodoisua(d.get_capid_theodoisua, sophieu.Text, l_id, int.Parse(r1["mabd"].ToString()), r1["ten"].ToString(), d_soluong_old, i_userid, "TT", "d_nhapct_" + s_mmyy, 0, 1, 0);
                    }
                }
                foreach (DataRow r1 in dtxoa.Rows)
                {
                    d.execute_data("delete from " + user + ".d_nhapct_" + s_mmyy + " where id=" + l_id + " and stt=" + int.Parse(r1["stt"].ToString()));
                    d.upd_theodoisua(d.get_capid_theodoisua, sophieu.Text, l_id, int.Parse(r1["mabd"].ToString()), r1["ten"].ToString(), d_soluong, i_userid, "TT", "d_nhapct_" + s_mmyy, 0, 0, 1);
                }
                foreach (DataRow r1 in dtthem.Rows)
                    d.upd_theodoisua(d.get_capid_theodoisua, sophieu.Text, l_id, int.Parse(r1["mabd"].ToString()), r1["ten"].ToString(), decimal.Parse(r1["soluong"].ToString()), i_userid, "TT", "d_nhapct_" + s_mmyy, 1, 0, 0);
				foreach(DataRow r1 in dtsave.Rows)
                    d.upd_tonkhoct_nhapct("delete", s_mmyy, ngayban.Text, l_id, int.Parse(r1["stt"].ToString()), decimal.Parse(r1["soluong"].ToString()), decimal.Parse(r1["sotien"].ToString()), int.Parse(r1["vat"].ToString()), int.Parse(makho.SelectedValue.ToString()), int.Parse(manguon.SelectedValue.ToString()), int.Parse(r1["id"].ToString()), r1["handung"].ToString(), r1["losx"].ToString(), int.Parse(r1["madv"].ToString()), "N", decimal.Parse(r1["giaban"].ToString()), decimal.Parse(r1["dongia"].ToString()), decimal.Parse(r1["sotienvat"].ToString()), 0);
			}
			foreach(DataRow r1 in dtct.Rows)
			{
                d.upd_nhapct(s_mmyy, l_id, int.Parse(r1["stt"].ToString()), int.Parse(r1["mabd"].ToString()),int.Parse(r1["madv"].ToString()), r1["handung"].ToString(), r1["losx"].ToString(), int.Parse(r1["vat"].ToString()), decimal.Parse(r1["soluong"].ToString()), decimal.Parse(r1["dongia"].ToString()), decimal.Parse(r1["sotien"].ToString()), decimal.Parse(r1["giaban"].ToString()), decimal.Parse(r1["giamua"].ToString()), decimal.Parse(r1["sl1"].ToString()), decimal.Parse(r1["sl2"].ToString()), decimal.Parse(r1["tyle"].ToString()), decimal.Parse(r1["giabancu"].ToString()), decimal.Parse(r1["giamuacu"].ToString()), 0, 0);
                d.upd_tonkhoct_nhapct("insert", s_mmyy, ngayban.Text, l_id, int.Parse(r1["stt"].ToString()), decimal.Parse(r1["soluong"].ToString()), decimal.Parse(r1["sotien"].ToString()), int.Parse(r1["vat"].ToString()), int.Parse(makho.SelectedValue.ToString()), int.Parse(manguon.SelectedValue.ToString()), int.Parse(r1["mabd"].ToString()), r1["handung"].ToString(), r1["losx"].ToString(), int.Parse(r1["madv"].ToString()), "N", decimal.Parse(r1["giaban"].ToString()), decimal.Parse(r1["dongia"].ToString()), decimal.Parse(r1["sotienvat"].ToString()), 0);
			}
            d.execute_data("update " + user + ".d_toathuocll_" + d.mmyy(ngayban.Text) + " set idduyet=" + l_id +" where mabn='"+sophieu.Text.Trim()+"'");
            d.updrec_nhapll(dtll, l_id, sophieu.Text, ngayban.Text, sophieu.Text, ngayban.Text, hoten.Text, i_madv, int.Parse(makho.SelectedValue.ToString()), int.Parse(manguon.SelectedValue.ToString()), namsinh.Text, "", 0);
			try
			{
				if (dtll.Rows.Count>0) cmbSophieu.SelectedIndex=dtll.Rows.Count-1;
			}
			catch{}
			ena_object(false);
			butMoi.Focus();
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
            try
            {
                if (dtll.Rows.Count > 0) l_id = long.Parse(cmbSophieu.SelectedValue.ToString());
                load_grid();
                ref_text();
            }
            catch { }
			ena_object(false);
			butMoi.Focus();
		}

		private void ngaysp_Validated(object sender, System.EventArgs e)
		{
            if (!d.bNgay(ngayban.Text.Trim()))
            {
                MessageBox.Show("Ngày không hợp lệ! Vui lòng nhập lại theo định dạng : dd/MM/yyyy", DAL.AccessData.Msg);
                ngayban.Focus();
                return;
            }
		}

		private void butThem_Click(object sender, System.EventArgs e)
		{
            load_dm();
            if (!KiemtraDetail()) return;
			tenbd.Enabled=true;
			mabd.Enabled=tenbd.Enabled;
			soluong.Enabled=tenbd.Enabled;
			dongia.Enabled=tenbd.Enabled;
			sotien.Enabled=tenbd.Enabled;
			giaban.Enabled=tenbd.Enabled;
            if (bEdit)
            {
                d.updrec_nhapct(dtthem, long.Parse(d.get_stt(dtthem).ToString()), i_mabd, mabd.Text, tenbd.Text, tenhc.Text, dvt.Text, "0000", "", i_madv, d_soluong, d_dongia, d_sotien, 0, d_sotien, d_giaban, d_dongia, d_soluong, 1, 0, d_giaban, d_dongia, d_sotien);
            }
            d.updrec_nhapct(dtct, long.Parse(d.get_stt(dtct).ToString()), i_mabd, mabd.Text, tenbd.Text, tenhc.Text, dvt.Text, "0000", "", i_madv, d_soluong, d_dongia, d_sotien, 0, d_sotien, d_giaban, d_dongia, d_soluong, 1, 0, d_giaban, d_dongia, d_sotien);
            dtct.AcceptChanges(); dataGrid1.Refresh();
			emp_detail();
			tenbd.Focus();
		}

		private void butXoa_Click(object sender, System.EventArgs e)
		{
            if (!KiemtraDetail()) return;
			i_mabd=d.get_iXuat(s_mmyy,int.Parse(makho.SelectedValue.ToString()),l_id,int.Parse(stt.Text));
			if (i_mabd!=0)
			{
				MessageBox.Show(tenbd.Text.Trim()+" "+dvt.Text.Trim()+"\nĐã xuất không cho phép hủy !",DAL.AccessData.Msg);
				return;
			}
            if (bEdit) d.updrec_nhapct(dtxoa, long.Parse(d.get_stt(dtxoa).ToString()), i_mabd, mabd.Text, tenbd.Text, tenhc.Text, dvt.Text, "0000", "", i_madv, d_soluong, d_dongia, d_sotien, 0, d_sotien, d_giaban, d_dongia, d_soluong, 1, 0, d_giaban, d_dongia, d_sotien);
            d.delrec(dtct, "stt=" + int.Parse(stt.Text));
			dtct.AcceptChanges();
			if (dtct.Rows.Count==0) emp_detail();
			else ref_text();
		}

		private void makho_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				if (makho.SelectedIndex==-1) makho.SelectedIndex=0;
				SendKeys.Send("{Tab}");
			}
		}

		private void mabd_Validated(object sender, System.EventArgs e)
		{
			if (mabd.Text!="")
			{
				r=d.getrowbyid(dtdmbd,"ma='"+mabd.Text+"'");
				if (r!=null) 
				{
                    i_madv = int.Parse(r["madv"].ToString());
					tenbd.Text=r["ten"].ToString();
					tenhc.Text=r["tenhc"].ToString();
					dvt.Text=r["dang"].ToString();
					d_dongia=decimal.Parse(r["dongia"].ToString());
					d_giaban=decimal.Parse(r["giaban"].ToString());
                    dongia.Text = d_dongia.ToString("###,###,###,##0.00");
                    giaban.Text = d_giaban.ToString("###,###,###,##0.00");
				}
			}
			if(!listDMBD.Focused) listDMBD.Hide();
		}

		private void listDMBD_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) mabd_Validated(null,null);
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text();
		}

		private void tinh_giatri()
		{
			try
			{
				d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
				d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
				d_sotien=Math.Round(d_soluong*d_dongia,3);
                sotien.Text = d_sotien.ToString("#,###,###,##0.00");
                if (d_dongia == 0)
                {
                    d_sotien = (sotien.Text != "") ? decimal.Parse(sotien.Text) : 0;
                    d_dongia = Math.Round(d_sotien / d_soluong, 3);
                }
                dongia.Text = d_dongia.ToString("#,###,###,##0.00");
                giaban.Text = d_dongia.ToString("#,###,###,##0.00");
			}
			catch{}
		}


		private void dongia_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
                dongia.Text = d_dongia.ToString("#,###,###,##0.00");
			}
			catch{dongia.Text="0";}
			tinh_giatri();
		}

		private void sotien_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_sotien=(sotien.Text!="")?decimal.Parse(sotien.Text):0;
                sotien.Text = d_sotien.ToString("#,###,###,##0.00");
			}
			catch{}
			tinh_giatri();
		}

		private void soluong_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
				soluong.Text=d_soluong.ToString("###,###,##0.0");
			}
			catch{}
			tinh_giatri();
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (cmbSophieu.Items.Count==0) return;
				if (d.bKhoaso(s_mmyy))
				{
					MessageBox.Show("Số liệu tháng"+s_mmyy.Substring(0,2)+" năm "+s_mmyy.Substring(2,2)+" đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống",DAL.AccessData.Msg);
					return;
				}
                if (dtdmbd.Rows.Count == 0) load_dm();
				foreach(DataRow r1 in dtct.Rows)
				{
					i_mabd=d.get_iXuat(s_mmyy,int.Parse(makho.SelectedValue.ToString()),l_id,int.Parse(r1["stt"].ToString()));
					if (i_mabd!=0)
					{
						r=d.getrowbyid(dtdmbd,"id="+i_mabd);
						if (r!=null)
						{
							MessageBox.Show(r["ten"].ToString().Trim()+" "+r["dang"].ToString().Trim()+"\nĐã xuất không cho phép hủy !",DAL.AccessData.Msg);
							return;
						}
					}
				}
				if (MessageBox.Show("Đồng ý hủy số phiếu này ?",DAL.AccessData.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
				{
					l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
                    foreach (DataRow r1 in dtct.Rows)
                    {
                        d.upd_tonkhoct_nhapct("delete", s_mmyy, ngayban.Text, l_id, int.Parse(r1["stt"].ToString()), decimal.Parse(r1["soluong"].ToString()), decimal.Parse(r1["sotien"].ToString()), int.Parse(r1["vat"].ToString()), int.Parse(makho.SelectedValue.ToString()), int.Parse(manguon.SelectedValue.ToString()), int.Parse(r1["mabd"].ToString()), r1["handung"].ToString(), r1["losx"].ToString(), int.Parse(r1["madv"].ToString()),"N", decimal.Parse(r1["giaban"].ToString()),decimal.Parse(r1["dongia"].ToString()), decimal.Parse(r1["sotienvat"].ToString()),0);
                        d.upd_theodoisua(d.get_capid_theodoisua, sophieu.Text, l_id, int.Parse(r1["mabd"].ToString()), r1["ten"].ToString(), decimal.Parse(r1["soluong"].ToString()), i_userid, "TT", "d_nhapct_" + s_mmyy, 0, 0, 1);
                    }
                    d.execute_data("delete from " + user + ".d_nhapct_" + s_mmyy + " where id=" + l_id);
                    d.execute_data("delete from " + user + ".d_nhapll_" + s_mmyy + " where id=" + l_id);
                    d.execute_data("update " + user + ".d_toathuocll_" + d.mmyy(ngayban.Text) + " set idduyet = 0  where mabn='" + sophieu.Text.Trim() + "'");
					d.delrec(dtll,"id="+l_id);
					cmbSophieu.Refresh();
					load_grid();
				}
			}
			catch{}
		}

		private void giaban_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
				d_giaban=(giaban.Text!="")?decimal.Parse(giaban.Text):0;
				giaban.Text=d_giaban.ToString("#,###,###,##0");
				if (d_giaban<d_dongia)
				{
                    MessageBox.Show("Giá bán không được nhỏ hơn giá mua !", DAL.AccessData.Msg);
					giaban.Focus();
					return;
				}
			}
			catch{giaban.Text="0";}
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{	
			if (dtct.Rows.Count==0) return;
			string s_tenkho=makho.Text;
			frmReport f=new frmReport(d,dtct,"","d_trathuoc.rpt",cmbSophieu.Text,hoten.Text,"","",ngayban.Text,s_userid,"","","","","","");
			//f.ShowDialog();
            this.FormMain.OpenTab("Trả Thuốc", f);
		}

		private DialogResult Thongso()
		{
			p.AllowSomePages = true;
			p.ShowHelp = true;
			p.Document = docToPrint;
			return p.ShowDialog();
		}

		private void tenbd_TextChanged(object sender, System.EventArgs e)
		{
            if (this.ActiveControl == tenbd && dtdmbd.Rows.Count > 0)
            {
                if (butMoi.Enabled) return;
                Filter_dmbd(tenbd.Text);
                listDMBD.BrowseToDmbd(tenbd, mabd, soluong, mabd.Location.X, mabd.Location.Y + mabd.Height - 2, mabd.Width + lTen.Width + tenbd.Width + lTenhc.Width + tenhc.Width - 15, mabd.Height + 5);
            }
		}

		private void mabd_TextChanged(object sender, System.EventArgs e)
		{
            if (this.ActiveControl == mabd && dtdmbd.Rows.Count > 0)
            {
                if (butMoi.Enabled) return;
                Filter_mabd(mabd.Text);
                listDMBD.BrowseToDmbd(mabd, tenbd, soluong, mabd.Location.X, mabd.Location.Y + mabd.Height - 2, mabd.Width + lTen.Width + tenbd.Width + lTenhc.Width + tenhc.Width - 15, mabd.Height + 5);
            }
		}

		private void Filter_mabd(string ma)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[listDMBD.DataSource];
				DataView dv=(DataView)cm.List;
				dv.RowFilter="ma like '%"+ma.Trim()+"%'";
			}
			catch{}
		}

		private void manguon_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				if (manguon.SelectedIndex==-1) manguon.SelectedIndex=0;
				SendKeys.Send("{Tab}{F4}");
			}
		}
        private void khoban_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                if (khoban.SelectedIndex == -1) khoban.SelectedIndex = 0;
                SendKeys.Send("{Tab}{F4}");
            }
        }

        private void bNut_SizeChanged(object sender, EventArgs e)
        {
            bNut.Location = new Point((this.Width - 628) / 2 + 1, (this.Height - 470) + 431);
        }

        private void ngayban_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void giaban_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                butThem.Focus();
            }
        }
	}
}
